package petrus.mood.items.photo
{
	import com.greensock.loading.display.ContentDisplay;
	import com.greensock.TweenLite;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import petrus.mood.items.photo.PhotoObject;
	
	/**
	 * ...
	 * @author Benoit Petrus
	 */
	public class PhotoItem extends Sprite
	{
		static private const ALPHA:Number = .3;
		static public const COLOR:Number = 0xFF0000;
		public var data:PhotoObject;
		private var _loader:Loader;
		private var RATIO:Number;
		private var gapX:int = 0;
		public var isLoaded:Boolean = false;
		public var W:int;
		public var H:int;
		public var W500:int;
		public var H500:int;
		
		public function PhotoItem()
		{
		}
		
		public function setData(value:XML):void
		{				
			data = new PhotoObject()
			data.URL1280 = value["photo-url"][0];
			data.URL500 = value["photo-url"][1];
			data.URL400 = value["photo-url"][2];
			data.URL250 = value["photo-url"][3];
			data.URL100 = value["photo-url"][4];
			data.URL75 = value["photo-url"][5];
			data.LINK = value["photo-link-url"];
			W = value.@width;
			H = value.@height;
			RATIO = W / H;
			W500 = 500;
			if (W < 500)
				W500 = W
			H500 = W500 / RATIO;
			gapX = (500 - W500) / 2
			
			//trace("[PhotoItem.setData] "+W500 , H500 , _data.URL500 , W);
			
			if (data.LINK)
				enable()
			drawLines(W500 - 2, H500 - 2)
			//loadImage()		
		}
		
		public function addImage(image:ContentDisplay):void
		{
			isLoaded = true
			
			image.x = gapX
			
			if (image.width < 500 && W500 == 500)
			{
				image.width = 500;
				image.height = 500 / RATIO
			}
			//image.alpha = 0
			addChild(image);
			//TweenLite.to(image, .25, {alpha:1 , onComplete : clearGraphics});
			//TweenLite.to(image, .0, { alpha:1 , onComplete : clearGraphics } );
			
			//alpha = 1 		
			clearGraphics();
		}
		
		//////////////////////////////////////////////////
		////////////////////////////		LINK
		
		private function enable():void
		{
			buttonMode = true
			addEventListener(MouseEvent.CLICK, gotoLink)
		}
		
		private function gotoLink(e:MouseEvent):void
		{
			navigateToURL(new URLRequest(data.LINK));
		}
		
		//////////////////////////////////////////////////
		////////////////////////////		GRAPHICS
		
		private function clearGraphics():void
		{
			graphics.clear();
		}
		
		private function drawLines(W:int, H:int):void
		{
			
			graphics.lineStyle(1, COLOR, ALPHA);
			graphics.beginFill(0xFF0000, 0)
			graphics.drawRect(gapX + 1, 0, W - 1, H);
			
			graphics.lineStyle(1, COLOR, ALPHA);
			graphics.moveTo(gapX, H);
			graphics.lineTo(W + gapX, 0);
			graphics.endFill()
		
		}
	
	}

}